﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;
using Kawanon.Usability;
using QTPlugin.Interop;
using System.Runtime.InteropServices;
using System.Security;
using Kawanon.QtHoney;

namespace TestUnit
{
	[ComImport, SuppressUnmanagedCodeSecurity, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	internal interface _IServiceProvider
	{
		void QueryService(ref Guid guid, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object Obj);
	}

	public partial class Form1 : Form
	{
		NListView m_listview;

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			//Kawanon.QtHoney.MainPlugin.Uninstall();

			GestureSettings.ExpressionsChanged += new EventHandler(GestureSettings_ExpressionsChanged);

			Kawanon.QtHoney.MainPlugin main = new Kawanon.QtHoney.MainPlugin();
			try
			{
				main.Open(null, null);
			}
			catch (Exception)
			{
			}
			main.Initialize(new int[] { 2,0,1 });

			string name = main.GetName(2);

			webBrowser1.Navigate(Application.StartupPath);

			//Form form = new Kawanon.QtHoney.GestureForm();
			//form.Show(this);

			//Kawanon.QtHoney.DebugForm f2 = new Kawanon.QtHoney.DebugForm();
			//f2.PropertyGrid.SelectedObject = f2;
			//f2.Show();

			//----
			//object o = Kawanon.QtHoney.DynamicReflectorFactory.Create(f2);
			//f2.PropertyGrid.SelectedObject = o;

			//Kawanon.QtHoney.MainPlugin plugin = new Kawanon.QtHoney.MainPlugin();
			//plugin.GetImage(true, 0);

			//Kawanon.QtHoney.PluginCustomItem item = new Kawanon.QtHoney.PluginItems.MergeWindowsButton();
			//Image image = item.GetImage(true);

			//image.ToString();


			//object[] src = new object[] { "123", 12, typeof(Object) };
			//var form = new Kawanon.QtHoney.ChooseFromArrayForm();
			//form.Source = src;
			//DialogResult res = form.ShowDialog();

			//form.ToString();
		}

		void GestureSettings_ExpressionsChanged(object sender, EventArgs e)
		{
			m_listview.ReleaseHandle();
			m_listview = null;

			timer1.Start();
		}

		private void ToolStripMenuItem_GestureForm_Click(object sender, EventArgs e)
		{
			Kawanon.QtHoney.GestureForm form = new Kawanon.QtHoney.GestureForm(null);
			form.Show(this);

		}

		private void menuItem1_Click(object sender, EventArgs e)
		{
			Kawanon.QtHoney.GestureForm form = new Kawanon.QtHoney.GestureForm(null);
			form.Show(this);
		}

		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			List<GestureExpression> list = new List<GestureExpression>();
			GestureSettings.LoadExpressions(list);

			GestureSettings.DumpExpressions(list);
		}

		private void Form1_Activated(object sender, EventArgs e)
		{
			ToolTip tooltip = new ToolTip();
			//tooltip.AutoPopDelay = 32767;
			//		tooltip.IsBalloon = true;
			//tooltip.Active = true;
			//	tooltip.UseFading = true;
			//tooltip.ShowAlways = true;
			//	tooltip.ToolTipTitle = "hoge!";
			//Dbg.Play();
			//tooltip.SetToolTip(listView1, "hoge");
			//tooltip.Show("hoge", Control.FromHandle(this.Handle), 0, 0, 20 * 1000);

		}

		private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
		{
			//NOTE: この時点ではアクティブなShellViewは変化していない (Internet Explorer_Server)

			timer1.Start();
		}

		private void timer1_Tick(object sender, EventArgs e)
		{
			Guid IID_IShellBrowser = new Guid("{000214E2-0000-0000-C000-000000000046}");

			_IServiceProvider provider = webBrowser1.ActiveXInstance as _IServiceProvider;

			object obj1;
			provider.QueryService(ref IID_IShellBrowser, ref IID_IShellBrowser, out obj1);
			IShellBrowser browser = (IShellBrowser)obj1;

			IShellView view;
			browser.QueryActiveShellView(out view);
			IntPtr hwndView;
			view.GetWindow(out hwndView);
			IntPtr listview = Utility.FindChildWindow(hwndView, Utility.EqualsClassName("SysListView32"));

			Kawanon.QtHoney.NListView nw = new Kawanon.QtHoney.NListView();
			nw.AssignHandle(listview);
			nw.StatusBarHandle = statusBar1.Handle;
			nw.ShellView = view;

			m_listview = nw;

			timer1.Stop();
		}
	}
}
